define(function(require, exports){
	var $ = require('jquery');
	var util = require('util');
	var pubjs = require('pubjs');
	var popwin = require('@base/dialog').base;
	var labels = require('@sys_labels').labels;

	var TabPanel = popwin.extend({
		init: function(config, parent){
			config = pubjs.conf(config, {
				'tab': util.clone(pubjs.config('defaultGroup')),
				'silence': false,
				'singleSelect':false,	//是否单选
				'default_metrics': [],
				'onlyWhisky': false,
				'module': '@base/dialog.base',
				'mask': 0,
				'autoShow': true,
				'showHead': false,
				'showFoot': false,
				'width': 'auto',
				'size': 'm',
				'hasScroll': false,
				'position':null,
				'style':{
					'layoutClass':'M-HighGridTabCustom'
				}
			});

			this.Super('init', arguments);
		},
		afterBuild: function(){
			this.Super('afterBuild', arguments);
			var c = this.getConfig();
			var data = this.getConfig('tab');
			var el = this.getContainer().css('min-height', 0);
			el.parent().css('min-height',0);

			var single = this.getConfig('singleSelect');
			// 创建弹框
			var popwin = $([
				'<div>',
					'<i class="M-HighGridTabCustomTriangle"/>',
					'<div class="M-HighGridTabCustomTable"><table><tr></tr></table></div>',
					'<div class="M-HighGridTabCustomFooter pb30" style="display: list-item">',
						'<input type="button" data-type="all" value="'+LANG('全选')+'"  class="btn btn-default"/>',
						'<input type="button" data-type="inverse" value="'+LANG('反选')+'"  class="ml10 btn btn-default"/>',
						'<input type="button" data-type="save" value="'+LANG('确认')+'" class="ml10 fr btn btn-success save"/>',
						'<input type="button" data-type="cancel" value="'+LANG('取消')+'" class="ml20 fr btn btn-default"/>',
						'<span class="ml5 fr reset">'+LANG('恢复默认')+'</span>',
					'</div>',
				'</div>'
			].join(''));

			if(single)
			{
				popwin.find('[data-type=all]').hide();
				popwin.find('[data-type=inverse]').hide();
			}

			var td, cols, metric;
			this.$tabDom = {};
			for (var e in data) {
				if(e != 'default' && data[e]){
					td = $('<td data-name="'+e+'"/>').appendTo(popwin.find('table tr'));
					this.$tabDom[e] = td;
					cols = data[e].cols;
					$('<strong>'+ LANG(data[e].text) +'</strong>').appendTo(td);
					for (var i = 0; i < cols.length; i++) {
						metric = cols[i];

						// 支持直接传字符串形式，从lables.js中读取对应配置
						if(util.isString(cols[i])){
							metric = labels.get(cols[i]);
						}

						$([
							'<label>',
							'<i class="custom-checkbox">',
							'<i class="icon-checkbox-in"/>',
							'</i>',
							'<input type="checkbox" data-text="'+(metric.text||cols[i])+'" data-name="'+(metric.name||cols[i])+'"/>',
							LANG(metric.text),
							'</label>'
						].join('')).appendTo(td);
					}
				}
			}
			popwin.appendTo(el);

			//input点击改变文字颜色
			this.uiProxy(this.$el, 'input', 'click', 'eventInputClick');
			// 绑定弹框按钮事件
			this.uiBind(popwin.find('.M-HighGridTabCustomFooter input[type="button"]'), 'click', 'eventButtonClick');
			this.uiBind(popwin.find('.M-HighGridTabCustomFooter .reset'), 'click', 'eventButtonResetDefault');

			// 设置弹框勾选值
			this.setValue();
			this.update();

			if(c.position && c.position.element){
				var anchor = c.position.element;
				popwin.find('.M-HighGridTabCustomTriangle').css({
					'top': -14,
					'left': anchor.offset().left - el.offset().left  + anchor.find('li').eq(0).width()
				}).show();
			}

			if(c.onlyWhisky)
			{
				this.onlyWhisky();
			}
		},
		showAll: function()
		{
			for(var prop in this.$tabDom)
			{
				if(this.$tabDom.hasOwnProperty(prop))
				{
					this.$tabDom[prop].show();
				}
			}
		},
		onlyWhisky: function()
		{
			for(var prop in this.$tabDom)
			{
				if(this.$tabDom.hasOwnProperty(prop) && ['back', 'conversion'].indexOf(prop) === -1)
				{
					this.$tabDom[prop].hide();
				}
			}
		},
		eventButtonClick: function(ev, elm){
			var type = $(elm).attr('data-type');
			var list = this.getDOM().find('table tr');
			var unsels, sels;
			switch(type){
				case 'save':
					this.onOk();
					break;
				case 'cancel':
					this.hide();
					this.fire('metricCancle');
					break;
				case 'all':
					unsels = list.find('input:not(:checked)');
					list.find(':checkbox').prop('checked', Boolean(unsels.length)).parent().addClass('change');
					break;
				case 'inverse':
					sels = list.find('input:checked');
					unsels = list.find('input:not(:checked)');
					sels.prop('checked', false).parent().removeClass('change');
					unsels.prop('checked', true).parent().addClass('change');
					break;
			}

			return false;
		},
		eventButtonResetDefault: function(ev, elm){
			this.setData(this.getConfig('defaultValue'));
			return false;
		},
		onOk: function(ev)
		{
			var value = this.getValue();
			if (!value.length) {
				return pubjs.alert('请选择指标');
			}
			var single = this.getConfig('singleSelect');
			var judge = single ? value : value.length;
			if(!judge)
			{
				return pubjs.alert('请先选择相应的指标');
			}

			this.hide();
			this.fire('metricSelect', single ? value[0] : value);
			return false;
		},
		eventInputClick: function(ev, elm){
			var dom = $(elm);
			if(dom.attr('data-name'))
			{
				if(this.getConfig('singleSelect'))
				{
					this.setValue([dom.attr('data-name')]);
				}
				else
				{
					dom.parent().toggleClass('change');
				}
			}
		},
		setData: function(data) {
			var value = [];
			if(data && data.length)
			{
				util.each(data, function(item) {
					if(item.id)
					{
						value.push(item.id);
					}
				});
			}

			if(value.length)
			{
				this.setValue(value);
			}

			return this;
		},
		setValue: function(value){
			var inputs = this.getDOM().find('table input');
			var item;
			for (var i = 0; i < inputs.length; i++) {
				item = $(inputs[i]);
				// value 中可能是对象也可能是字符串
				var checked = util.find(value, item.attr('data-name')) || util.find(value, item.attr('data-name'), 'name');
				// var checked = util.find(value, item.attr('data-name'));
				item.prop('checked', Boolean(checked));
				item.parent().toggleClass('change', Boolean(checked));
			}

			return this;
		},
		getValue: function(){
			var input = this.getDOM().find('table input:checked');
			var data = [];
			for (var i = 0; i < input.length; i++) {
				data.push({
					id: $(input[i]).attr('data-name'),
					name: $(input[i]).attr('data-text')
				});
			}

			return data;
		},
		reset: function()
		{
			this.setValue([]);
			return this;
		}
	});

	exports.base = TabPanel;
});